﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions cursus/periode/subject

        #region list init

        //init the cursus_listBox
        private void list_cursus(ListBox lst)
        {
            lst.Items.Clear();

            foreach (cursus c in (from e in db_remote.cursus select e))
            {
                lst.Items.Add(c.name);
            }
        }

        //init the periods_listBox
        private void list_periods(ListBox lst, int id_cursus)
        {
            lst.Items.Clear();
            foreach (period p in (from e in db_remote.periods
                                  where e.cursus_id == id_cursus
                                  select e))
            {
                string name_period_temp = p.name_period;
                string[] name_period = name_period_temp.Split('?');
                lst.Items.Add(name_period[1]);
            }
        }

        //init the subjects_listBox
        private void list_subjects(ListBox lst, int period_id)
        {
            lst.Items.Clear();
            foreach (subjects s in (from e in db_remote.subjects
                                    where e.id_period == period_id
                                    select e))
            {
                lst.Items.Add(s.name);
            }
        }

        #endregion

        #region retrieve infos

        //get the ID of the passed cursus name
        private int get_id_cursus(string name_cursus)
        {
            cursus c = (from e in db_remote.cursus
                        where e.name == (string)name_cursus
                        select e).FirstOrDefault();
            return c.id;

        }

        //get the ID of the passed cursus name
        private int get_id_period(string name_period, string name_cursus)
        {
            int cursus = get_id_cursus(name_cursus);
            period p = (from e in db_remote.periods
                        where e.name_period == name_period & e.cursus_id == cursus
                        select e).FirstOrDefault();
            return p.id;
        }

        //get the ID of the passed subjects name
        private int get_id_subject(string name_subject, string name_cursus, string name_period)
        {
            int period = get_id_period(name_period, name_cursus);
            subjects s = (from e in db_remote.subjects
                          where e.name == name_subject & e.id_period == period
                          select e).FirstOrDefault();
            return s.id;
        }

        #endregion

        #region add

        private void add_cursus()
        {
            if (cursus_comment_textbox.Text != "" && cursus_name_textbox.Text != "")
            {
                cursus cur = new cursus();
                cur.comment = cursus_comment_textbox.Text;
                cur.name = cursus_name_textbox.Text;
                cur.t_end = cursus_endingdate_datepicker.SelectedDate;

                db_remote.cursus.InsertOnSubmit(cur);
                db_remote.SubmitChanges();
            }
        }

        private void add_period()
        {
            if (period_comment_textbox.Text != "" && period_comment_textbox.Text != ""
                && period_end_datepicker.Text != "" && period_start_datepicker.Text != "")
            {
                period per = new period();
                per.comment = period_comment_textbox.Text;

                int cursus_id_temp = get_id_cursus(cursus_listbox.SelectedValue.ToString());

                per.cursus_id = cursus_id_temp;

                //on concatène l'id du cursus en début de nom de la période
                per.name_period = cursus_id_temp.ToString() + "?" + period_name_textbox.Text;

                per.t_begin = Convert.ToDateTime(period_start_datepicker.Text + " 00:00:00");
                per.t_end = Convert.ToDateTime(period_end_datepicker.Text + " 00:00:00");

                db_remote.periods.InsertOnSubmit(per);
                db_remote.SubmitChanges();
            }
        }

        private void add_subject()
        {
            if (subject_elearning_textbox.Text != "" && subject_eval_textbox.Text != ""
                & subject_facetoface_textbox.Text != "" && subject_pratical_textbox.Text != ""
                & subject_name_textbox.Text != "")
            {
                subjects sub = new subjects();
                sub.id_period = get_id_period(periods_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString());
                sub.name = subject_name_textbox.Text;

                sub.nb_hours_e_learning = Convert.ToInt32(subject_elearning_textbox.Text);
                sub.nb_hours_exams = Convert.ToInt32(subject_eval_textbox.Text);
                sub.nb_hours_face_to_face = Convert.ToInt32(subject_facetoface_textbox.Text);
                sub.nb_hours_practical_work = Convert.ToInt32(subject_pratical_textbox.Text);

                db_remote.subjects.InsertOnSubmit(sub);
                db_remote.SubmitChanges();
            }
        }

        #endregion

        #region update

        private void update_cursus()
        {
            cursus cur = (from e in db_remote.cursus
                          where e.name == cursus_listbox.SelectedValue.ToString()
                          select e).FirstOrDefault();
            cur.comment = cursus_comment_textbox.Text;
            cur.name = cursus_name_textbox.Text;
            cur.t_end = cursus_endingdate_datepicker.SelectedDate;

            db_remote.SubmitChanges();
        }

        private void update_period()
        {
            int cursus_id_temp = get_id_cursus(cursus_listbox.SelectedValue.ToString());

            period per = (from e in db_remote.periods
                          where e.id == get_id_period(cursus_id_temp + "?" + periods_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString())
                          select e).FirstOrDefault();

            per.cursus_id = cursus_id_temp;

            //on concatène l'id du cursus en début de nom de la période
            per.name_period = cursus_id_temp.ToString() + "?" + period_name_textbox.Text;

            per.comment = period_comment_textbox.Text;
            per.t_begin = Convert.ToDateTime(period_start_datepicker.Text + " 00:00:00");
            per.t_end = Convert.ToDateTime(period_end_datepicker.Text + " 00:00:00");
            db_remote.SubmitChanges();
        }

        private void update_subject()
        {

            int cursus_id_temp = (from e in db_remote.cursus
                                  where e.name == cursus_listbox.SelectedValue.ToString()
                                  select e.id).FirstOrDefault();

            subjects sub = (from e in db_remote.subjects
                            where e.id == get_id_subject(subjects_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString(), cursus_id_temp + "?" + periods_listbox.SelectedValue.ToString())
                            select e).FirstOrDefault();
            sub.id_period = get_id_period(periods_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString());
            sub.name = subject_name_textbox.Text;

            sub.nb_hours_e_learning = Convert.ToInt32(subject_elearning_textbox.Text);
            sub.nb_hours_exams = Convert.ToInt32(subject_eval_textbox.Text);
            sub.nb_hours_face_to_face = Convert.ToInt32(subject_facetoface_textbox.Text);
            sub.nb_hours_practical_work = Convert.ToInt32(subject_pratical_textbox.Text);

            db_remote.SubmitChanges();
        }


        #endregion

        #region delete

        private void delete_subject(int id)
        {
            subjects sub = (from e in db_remote.subjects
                            where e.id == id
                            select e).FirstOrDefault();
            db_remote.subjects.DeleteOnSubmit(sub);
            db_remote.SubmitChanges();

            list_subjects(subjects_listbox, get_id_period(periods_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString()));
        }

        private void delete_period(int id)
        {
            //Delete the subjects of period
            foreach (subjects s in (from e in db_remote.subjects
                                    where e.id_period == id
                                    select e))
            {
                db_remote.subjects.DeleteOnSubmit(s);
            }

            subjects_listbox.Items.Clear();

            //Delete the period
            period per = (from e in db_remote.periods
                          where e.id == id
                          select e).FirstOrDefault();
            db_remote.periods.DeleteOnSubmit(per);
            db_remote.SubmitChanges();

            //Refresh the period list
            list_periods(periods_listbox, get_id_cursus(cursus_listbox.SelectedValue.ToString()));
        }

        private void delete_cursus(int id)
        {
            //Delete the periods of this cursus
            foreach (period p in (from e in db_remote.periods
                                  where e.cursus_id == id
                                  select e))
            {
                foreach (subjects s in (from f in db_remote.subjects
                                        where f.id_period == p.id
                                        select f))
                {
                    db_remote.subjects.DeleteOnSubmit(s);
                }

                db_remote.periods.DeleteOnSubmit(p);
            }

            periods_listbox.Items.Clear();
            subjects_listbox.Items.Clear();

            //Delete the cursus
            cursus cur = (from e in db_remote.cursus
                          where e.id == id
                          select e).FirstOrDefault();
            db_remote.cursus.DeleteOnSubmit(cur);
            db_remote.SubmitChanges();

            //Refresh the cursus list
            list_cursus(cursus_listbox);
        }

        #endregion

        #region populate/clear information field

        private void populate_cursus_fields()
        {
            cursus cur = (from e in db_remote.cursus
                          where e.name == cursus_listbox.SelectedValue.ToString()
                          select e).FirstOrDefault();

            cursus_name_textbox.Text = cur.name;
            cursus_comment_textbox.Text = cur.comment;
            cursus_endingdate_datepicker.SelectedDate = cur.t_end;
        }

        private void clean_cursus_fields()
        {
            cursus_name_textbox.Text = "";
            cursus_comment_textbox.Text = "";
            cursus_endingdate_datepicker.Text = "";
        }

        private void populate_period_fields()
        {

            int cursus_id_temp = (from e in db_remote.cursus
                                  where e.name == cursus_listbox.SelectedValue.ToString()
                                  select e.id).FirstOrDefault();

            period per = (from e in db_remote.periods
                          where e.id == get_id_period(cursus_id_temp + "?" + periods_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString())
                          select e).FirstOrDefault();


            string period_name_full = per.name_period;
            string[] period_name = period_name_full.Split('?');

            period_name_textbox.Text = period_name[1];
            period_comment_textbox.Text = per.comment;

            string[] startingdate_part = per.t_begin.ToString().Split(' ');
            string[] endingdate_part = per.t_end.ToString().Split(' ');

            period_start_datepicker.Text = startingdate_part[0];
            period_end_datepicker.Text = endingdate_part[0];
        }

        private void clean_period_fields()
        {
            period_name_textbox.Text = "";
            period_comment_textbox.Text = "";
            period_start_datepicker.Text = DateTime.Now.ToString();
            period_end_datepicker.Text = DateTime.Now.ToString();
        }

        private void populate_subject_fields()
        {

            int cursus_id_temp = (from e in db_remote.cursus
                                  where e.name == cursus_listbox.SelectedValue.ToString()
                                  select e.id).FirstOrDefault();

            subjects sub = (from e in db_remote.subjects
                            where e.id == get_id_subject(subjects_listbox.SelectedValue.ToString(), cursus_listbox.SelectedValue.ToString(), cursus_id_temp + "?" + periods_listbox.SelectedValue.ToString())
                            select e).FirstOrDefault();

            subject_name_textbox.Text = sub.name;
            subject_elearning_textbox.Text = sub.nb_hours_e_learning.ToString();
            subject_eval_textbox.Text = sub.nb_hours_exams.ToString();
            subject_facetoface_textbox.Text = sub.nb_hours_face_to_face.ToString();
            subject_pratical_textbox.Text = sub.nb_hours_practical_work.ToString();
        }

        private void clean_subject_fields()
        {
            subject_name_textbox.Text = "";
            subject_elearning_textbox.Text = "";
            subject_eval_textbox.Text = "";
            subject_facetoface_textbox.Text = "";
            subject_pratical_textbox.Text = "";
        }

        #endregion

        #endregion
    }
}
